package com.ruoyi.app.controller;

import java.util.List;

import com.ruoyi.common.core.domain.bo.LoginAppUser;
import com.ruoyi.common.core.domain.bo.LoginUser;
import com.ruoyi.common.satoken.utils.LoginHelper;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.idempotent.annotation.RepeatSubmit;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.web.core.BaseController;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.app.domain.vo.AppMailboxMessageVo;
import com.ruoyi.app.domain.bo.AppMailboxMessageBo;
import com.ruoyi.app.service.AppMailboxMessageService;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;

/**
 * 用户信件
 *
 * @author zcc
 * @date 2024-12-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/app/appmessage")
public class AppMailboxMessageController extends BaseController {

    private final AppMailboxMessageService appMailboxMessageService;

    /**
     * 查询用户信件列表
     */
    @GetMapping("/list")
    public TableDataInfo<AppMailboxMessageVo> list(AppMailboxMessageBo bo, PageQuery pageQuery) {
        LoginAppUser loginUser = LoginHelper.getLoginAppUser();
        bo.setAuthorId(loginUser.getUserId());
        return appMailboxMessageService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出用户信件列表
     */
    @SaCheckPermission("app:appmessage:export")
    @Log(title = "用户信件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppMailboxMessageBo bo, HttpServletResponse response) {
        List<AppMailboxMessageVo> list = appMailboxMessageService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户信件", AppMailboxMessageVo.class, response);
    }

    /**
     * 获取用户信件详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("app:appmessage:query")
    @GetMapping("/{id}")
    public R<AppMailboxMessageVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(appMailboxMessageService.queryById(id));
    }

    /**
     * 新增用户信件
     */
    @Log(title = "用户信件", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody AppMailboxMessageBo bo) {
        LoginAppUser loginUser = LoginHelper.getLoginAppUser();
        if(loginUser == null){
            return R.warn("请重新登录");
        }
        bo.setAuthorId(loginUser.getUserId());
        Long id = appMailboxMessageService.insertById(bo);
        return R.ok("成功",id);
    }

    /**
     * 修改用户信件
     */
    @SaCheckPermission("app:appmessage:edit")
    @Log(title = "用户信件", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppMailboxMessageBo bo) {
        appMailboxMessageService.updateByBo(bo);
        return R.ok();
    }

    /**
     * 删除用户信件
     *
     * @param ids 主键串
     */
    @SaCheckPermission("app:appmessage:remove")
    @Log(title = "用户信件", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        appMailboxMessageService.deleteByIds(List.of(ids));
        return R.ok();
    }
}
